<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">



        <div class="container" >
          <el-form :inline="true" size="small">
            <el-form-item label="使用状态：" class="mr10">
              <el-select v-model="data.status" placeholder="请选择状态" class="selWidth" @change="getIssueList">
                <el-option label="全部" value=""/>
                <el-option label="已使用" value="1"/>
                <el-option label="未使用" value="0"/>
              </el-select>
            </el-form-item>
            <el-form-item label="核销码：" class="mr10">
              <el-input v-model="data.code" @keyup.enter.native="getIssueList" placeholder="请输入核销码"
                        class="selWidth"
              >
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getIssueList"/>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <div class="container" >
          <el-form :inline="true" size="small">

            <el-form-item  class="mr10">
              <el-input v-model="merData.number"  placeholder="请输入核销码的数量"
                        class="selWidth" type="number"
              >
                <el-button slot="append" class="el-button-solt" @click="createVerification" style="color: #fff;" >新建核销码</el-button>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="Loading"
        :data="issueData.data"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="80"
        />
        <el-table-column
          prop="code"
          label="核销码"
          min-width="150"
        />
        <el-table-column
          label="使用状态"
          min-width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">未使用</span>
            <span v-else>已使用</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_id"
          label="订单id"
          min-width="100"
        />

        <el-table-column
          prop="create_time"
          label="使用时间"
          min-width="150"
        />


      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFromIssue.limit"
          :current-page="data.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="issueData.total"
          @size-change="handleSizeChangeIssue"
          @current-change="pageChangeIssue"
        />
      </div>
    </el-card>
  </div>
</template>

<script>

import { issueApi, getVerificationList, createVerification } from '@/api/marketing'
import { roterPre } from '@/settings'

export default {
  name: 'VerificationList',
  filters: {
    failFilter(status) {
      const statusMap = {
        'receive': '自己领取',
        'send': '后台发送',
        'give': '满赠',
        'new': '新人',
        'buy': '买赠送'
      }
      return statusMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        0: '未使用',
        1: '已使用'
      }
      this.data.status = statusMap[status]
      return statusMap[status]
    }
  },
  data() {
    return {
      merData: {
        number: null
      },
      data: {
        status: null,
        title: '',
        page: 1,
        code: '',
        limit: 10
      },
      Loading: false,
      roterPre: roterPre,
      tableFromIssue: {
        page: 1,
        limit: 10,
        coupon: '',
        status: '',
        username: ''
      },
      issueData: {
        data: [],
        total: 0
      }
    }
  },
  mounted() {
    this.getIssueList()
  },
  methods: {
    createVerification(){
      createVerification(this.merData).then(res => {
        this.getIssueList(1)
      }).catch(res => {
        this.$message.error(res.message)
      })
    },
    // 列表
    getIssueList(num) {
      this.Loading = true
      if(num){
        this.data.page=num
      }
      getVerificationList(this.data).then(res => {
        this.issueData.data = res.data.list
        this.issueData.total = res.data.count
        this.Loading = false
      }).catch(res => {
        this.Loading = false
        this.$message.error(res.message)
      })
    },
    pageChangeIssue(page) {
      this.data.page = page
      this.getIssueList()
    },
    handleSizeChangeIssue(val) {
      this.data.limit = val
      this.getIssueList()
    }
  }
}
</script>

<style scoped lang="scss">
.box-card {
  /deep/ .el-card__header {
    padding: 18px 20px 0 18px !important;
  }
}

.selWidth {
  width: 300px !important;
}

.seachTiele {
  line-height: 35px;
}

.fa {
  color: #0a6aa1;
  display: block;
}

.sheng {
  color: #ff0000;
  display: block;
}
</style>
